package com.chuyu.jzwboa.utils.popupwindow;

import android.animation.ObjectAnimator;
import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.chuyu.jzwboa.R;
import com.chuyu.jzwboa.utils.sys.ScreenUtils;

import static com.facebook.fresco.helper.utils.DensityUtil.dip2px;

/**
 * Created by Administrator on 2017/12/14.
 */

public class PopupWindowUtil {

	private View rootVew;
	private PopupWindow popupWindow;
	private ImageView iv_cancel;
	private LinearLayout layout_sendteamwork, layout_writelog, layout_uploadfile, layout_sendmsg, layout_writemail;

	public static PopupWindowUtil getInstance() {
		return PopupWindowUtil.MenuUtilHolder.INSTANCE;
	}

	private static class MenuUtilHolder {
		public static PopupWindowUtil INSTANCE = new PopupWindowUtil();
	}

	/**
	 * 动画执行的 属性值数组
	 */
	float animatorProperty[] = null;
	/**
	 * 第一排图 距离屏幕底部的距离
	 */
	int top = 0;
	/**
	 * 第二排图 距离屏幕底部的距离
	 */
	int bottom = 0;

	private void _createView(final Context context) {
		rootVew = LayoutInflater.from(context).inflate(R.layout.filemanage_popupwindow, null);
		popupWindow = new PopupWindow(rootVew,
				ScreenUtils.getScreenWidth(context), ScreenUtils.getScreenHeight(context));
		//设置为失去焦点 方便监听返回键的监听
		popupWindow.setFocusable(false);

		// 如果想要popupWindow 遮挡住状态栏可以加上这句代码
		popupWindow.setClippingEnabled(false);
		popupWindow.setBackgroundDrawable(new BitmapDrawable());
		popupWindow.setOutsideTouchable(false);
//		popupWindow.setInputMethodMode(PopupWindow.INPUT_METHOD_NEEDED);
//		popupWindow.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
//		popupWindow.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
		if (animatorProperty == null) {
			top = dip2px(context, 310);
			bottom = dip2px(context, 210);
			animatorProperty = new float[]{bottom, 60, -30, -20 - 10, 0};
		}

		initLayout(context);
	}

	private void initLayout(Context context) {
		iv_cancel = (ImageView) rootVew.findViewById(R.id.iv_cancel);
		layout_sendteamwork = (LinearLayout) rootVew.findViewById(R.id.layout_sendteamwork);
		layout_writelog = (LinearLayout) rootVew.findViewById(R.id.layout_writelog);
		layout_uploadfile = (LinearLayout) rootVew.findViewById(R.id.layout_uploadfile);
		layout_sendmsg = (LinearLayout) rootVew.findViewById(R.id.layout_sendmsg);
		layout_writemail = (LinearLayout) rootVew.findViewById(R.id.layout_writemail);

		iv_cancel.setOnClickListener(new PopupWindowUtil.MViewClick(0, context));

		layout_sendteamwork.setOnClickListener(new PopupWindowUtil.MViewClick(1, context));
		layout_writelog.setOnClickListener(new PopupWindowUtil.MViewClick(2, context));
		layout_uploadfile.setOnClickListener(new PopupWindowUtil.MViewClick(3, context));
		layout_sendmsg.setOnClickListener(new PopupWindowUtil.MViewClick(4, context));
		layout_writemail.setOnClickListener(new PopupWindowUtil.MViewClick(5, context));

	}
	private class MViewClick implements View.OnClickListener {

		public int index;
		public Context context;

		public MViewClick(int index, Context context) {
			this.index = index;
			this.context = context;
		}

		@Override
		public void onClick(View v) {
			if (index == 0) {
				//加号按钮点击之后的执行
				_rlClickAction();
			} else {
				showToast(context, "index=" + index);
			}
		}
	}

	Toast toast = null;
	/**
	 * 防止toast 多次被创建
	 *
	 * @param context context
	 * @param str     str
	 */
	private void showToast(Context context, String str) {
		if (toast == null) {
			toast = Toast.makeText(context, str, Toast.LENGTH_SHORT);
		} else {
			toast.setText(str);
		}
		toast.show();
	}

	/**
	 * 刚打开popupWindow 执行的动画
	 */
	private void _openPopupWindowAction() {
		ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(iv_cancel, "rotation", 0f, 135f);
		objectAnimator.setDuration(200);
		objectAnimator.start();

		_startAnimation(layout_sendteamwork, 500, animatorProperty);
		_startAnimation(layout_writelog, 430, animatorProperty);
		_startAnimation(layout_uploadfile, 430, animatorProperty);
		_startAnimation(layout_sendmsg, 500, animatorProperty);

		_startAnimation(layout_writemail, 500, animatorProperty);
	}
	/**
	 * 关闭 popupWindow执行的动画
	 */
	public void _rlClickAction() {
		if (iv_cancel != null) {

			ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(iv_cancel, "rotation", 135f, 0f);
			objectAnimator.setDuration(300);
			objectAnimator.start();

			_closeAnimation(layout_sendteamwork, 300, top);
			_closeAnimation(layout_writelog, 200, top);
			_closeAnimation(layout_uploadfile, 200, top);
			_closeAnimation(layout_sendmsg, 300, top);
			_closeAnimation(layout_writemail, 300, bottom);
			iv_cancel.postDelayed(new Runnable() {
				@Override
				public void run() {
					_close();
				}
			}, 300);

		}
	}
	/**
	 * 弹起 popupWindow
	 *
	 * @param context context
	 * @param parent  parent
	 */
	public void _show(Context context, View parent) {
		_createView(context);
		if (popupWindow != null && !popupWindow.isShowing()) {
			popupWindow.showAtLocation(parent, Gravity.NO_GRAVITY, 0, 0);
			_openPopupWindowAction();
		}
	}
	/**
	 * 关闭popupWindow
	 */

	public void _close() {
		if (popupWindow != null && popupWindow.isShowing()) {
			popupWindow.dismiss();
			popupWindow = null;
		}
	}

	/**
	 * @return popupWindow 是否显示了
	 */
	public boolean _isShowing() {
		if (popupWindow == null) {
			return false;
		} else {
			return popupWindow.isShowing();
		}
	}

	/**
	 * 关闭 popupWindow 时的动画
	 *
	 * @param view     mView
	 * @param duration 动画执行时长
	 * @param next     平移量
	 */
	private void _closeAnimation(View view, int duration, int next) {
		ObjectAnimator anim = ObjectAnimator.ofFloat(view, "translationY", 0f, next);
		anim.setDuration(duration);
		anim.start();
	}

	/**
	 * 启动动画
	 *
	 * @param view     view
	 * @param duration 执行时长
	 * @param distance 执行的轨迹数组
	 */
	private void _startAnimation(View view, int duration, float[] distance) {
		ObjectAnimator anim = ObjectAnimator.ofFloat(view, "translationY", distance);
		anim.setDuration(duration);
		anim.start();
	}
}
